﻿@using Smartstore.Web.Models.Cart;

@model ShoppingCartModel

@{
    var hasItems = Model.Items.Any();
    var checkoutAllowed = Model.Items.Any(x => x.Active);
    var shippingInfoUrl = await Url.TopicAsync("ShippingInfo", true);

    var paymentButtons = hasItems
        ? (await Model.ButtonPaymentMethods.Items
            .SelectAwait(x => x.InvokeAsync(ViewContext))
            .AsyncToList())
            .Where(x => x.HasContent())
        : Enumerable.Empty<IHtmlContent>();
}

<div class="order-summary-content cart-content"
     data-empty-text="@T("ShoppingCart.CartIsEmpty")"
     data-update-item-url="@Url.Action("UpdateCartItem", "ShoppingCart")">
    <zone name="order_summary_content_before" />

    @if (Model.DisplayCommentBox && !Model.IsEditable)
    {
        <partial name="_CommentBox" model="Model" />
    }

    @if (hasItems)
    {
        <div id="cart-warnings">
            <partial name="CartWarnings" model="Model.Warnings" />
        </div>

        @* "File upload" attribute control type requries "multipart/form-data" *@
        <form asp-route="ShoppingCart" id="frm-shopping-cart" method="post" enctype="multipart/form-data">
            <div class="row cart-container">
                @* Cart Items *@
                <div class="col-lg-7 col-xl-8">
                    <div id="cart-items" class="cart" attr-class='(Model.IsEditable, "cart-editable")'>
                        <div class="cart-body">
                            <partial name="CartItems" model="Model" />
                        </div>
                    </div>
                </div>
                @* Cart Summary *@
                <div class="col-lg-5 col-xl-4">
                    <div id="cart-select-products-warning" class="alert alert-warning@(checkoutAllowed ? " d-none" : string.Empty)">
                        @Html.Raw(Model.CheckoutNotAllowedWarning)
                    </div>
                    <div id="cart-summary-container" class="cart-footer@(checkoutAllowed ? string.Empty : " d-none")">
                        <div class="row">
                            <div class="col-12 cart-footer-group">
                                @if (Model.IsEditable)
                                {
                                    <partial name="_CheckoutAttributes" model="Model.CheckoutAttributes" view-data="new ViewDataDictionary(ViewData)" />
                                }

                                <zone name="order_summary_totals_before" />

                                <div id="order-totals">
                                    @await Component.InvokeAsync("OrderTotals", new { isEditable = Model.IsEditable })
                                </div>

                                <zone name="order_summary_totals_after" />
                            </div>
                            <div class="col-12 cart-footer-group" sm-suppress-if-empty>
                                @if (Model.IsEditable)
                                {
                                    <zone name="order_summary_actions_before" />

                                    <partial name="_DiscountBox" model="Model.DiscountBox" />
                                    <partial name="_GiftCardBox" model="Model.GiftCardBox" />
                                    <partial name="_RewardPointsBox" model="Model.RewardPoints" />
                                    <partial name="EstimateShipping" model="Model.EstimateShipping" />

                                    <zone name="order_summary_actions_after" />
                                }
                                else
                                {
                                    <div sm-if="Model.CheckoutAttributeInfo.HasValue()" class="selected-checkout-attributes">
                                        @Html.Raw(Model.CheckoutAttributeInfo)
                                    </div>
                                }
                            </div>

                            <zone name="order_summary_actions" remove-if-empty="true" replace-content="true">
                                <div sm-if="Model.IsEditable" class="col-12 mt-2 cart-footer-actions">
                                    <zone name="order_summary_buttons_before" />

                                    <div class="cart-buttons">
                                        <div id="start-checkout-buttons" class="d-flex flex-column row-gap-2">
                                            <div class="checkout-buttons">
                                                <input type="submit" name="startcheckout" value="startcheckout" id="startcheckout" class="d-none" />
                                                <button type="button" id="checkout" name="checkout" class="btn btn-primary btn-lg btn-block btn-checkout">
                                                    <span>@T("Checkout.Button")</span>
                                                    <i class="fa fa-angle-right"></i>
                                                </button>
                                            </div>
                                            <div id="cart-payment-buttons">
                                                <partial name="CartPaymentButtons" model="Model" />
                                            </div>
                                        </div>
                                    </div>

                                    <zone name="order_summary_buttons_after" />
                                </div>
                            </zone>
                        </div>
                    </div>
                </div>

                @* Back Button *@
                <div sm-if="Model.IsEditable" class="col-12 col-md-6 col-lg-7 col-xl-4 col-xxl-3 my-3">
                    <button class="btn btn-secondary btn-lg btn-block btn-continue-shopping" type="submit" name="continueshopping" value="continueshopping">
                        <i class="fa fa-angle-left mr-3"></i>
                        <span>@T("ShoppingCart.ContinueShopping")</span>
                    </button>
                </div>
            </div>

        </form>

        <zone name="order_summary_content_after" />

        if (Model.IsEditable)
        {
            @await Component.InvokeAsync("CrossSellProducts")
        }
    }
    else
    {
        <div class="alert alert-warning fade show">
            @T("ShoppingCart.CartIsEmpty")
        </div>
    }
</div>

@if (!Model.IsEditable && Model.Items.Any(x => x.IsDownload && x.HasUserAgreement))
{
    <modal id="user-agreement-modal" sm-render-at-page-end="true" sm-center-vertically="true" sm-size="FlexSmall">
        <modal-body></modal-body>
        <modal-footer>
            <button class="btn btn-secondary" data-dismiss="modal">@T("Common.Confirm")</button>
        </modal-footer>
    </modal>

    <script sm-target-zone="scripts" data-origin="order-summary">
        $(function () {
            $('a.download-user-agreement').on("click", function () {
                const url = '@(Url.Action("GetUserAgreement", "Download"))?productId=' + $(this).attr('data-id');
                const dialog = $('#user-agreement-modal');

                dialog.find('.modal-body').empty();

                $.get(url, function (html) {
                    dialog.find('.modal-body').html(html);
                });

                dialog.modal();
                return false;
            });
        });
    </script>
}

@if (shippingInfoUrl.HasValue())
{
    <modal id="shipping-info-modal" sm-render-at-page-end="true" sm-size="FlexSmall">
        <modal-body>
            <iframe src="@shippingInfoUrl" frameBorder="0" class="h-100"></iframe>
        </modal-body>
        <modal-footer>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">@T("Common.Close")</button>
        </modal-footer>
    </modal>
}

@if (Model.IsEditable)
{
    <script src="~/js/public.refreshcart.js" sm-target-zone="scripts"></script>

    <script sm-target-zone="scripts">
        $("#checkout").on("click", function () {
            var form = $("#frm-shopping-cart");

            if (!form[0].reportValidity()) {
                return false;
            }

            $('#startcheckout').trigger('click');
            return false;
        });
    </script>
}

<script sm-target-zone="scripts" data-origin="order-summary">
    $(function () {
        $('#frm-shopping-cart').on('keydown', function (e) {
            return e.key !== 'Enter' || e.target.tagName.toLowerCase() == 'textarea';
        });
    });
</script>
